﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Horridor
{
    /// <summary>
    /// The level class
    /// </summary>
    class Level
    {
        // List of players
        private Player player1;

        // List of decorations,
        private List<DrawableObject> decorations = new List<DrawableObject>();
        private List<InteractiveObject> collidableObjects = new List<InteractiveObject>();
        private List<Enemy> enemies = new List<Enemy>();

        // Level settings
        private int xSize = 60;
        private int ySize = 60;

        // Tile textures
        private int textureSize = 128;
        private Texture2D[] tileTextures = null;
        private Rectangle[] tiles = null;

        /// <summary>
        /// Level
        /// </summary>
        public Level(Texture2D[] textures)
        {
            // Set the basic information
            player1 = new Player(Game1.playerTex);
            tileTextures = textures;
            tiles = new Rectangle[xSize * ySize];

            // Generate the map
            GenerateMap();
        }

        /// <summary>
        /// Generate the map
        /// </summary>
        private void GenerateMap()
        {
            // x: 0-1280 y: 0-768
            int x = 0;
            int y = 0;
            for (int i = 0; i < xSize * ySize; i++)
            {
                tiles[i] = new Rectangle(x, y, textureSize, textureSize);

                x += textureSize;
                if (x == xSize * textureSize)
                {
                    x = 0;
                    y += textureSize;
                }
            }
        }


        /// <summary>
        /// Update
        /// </summary>
        /// <param name="elaspedMiliseconds"></param>
        public void Update(int elaspedMiliseconds)
        {
            player1.Update(elaspedMiliseconds);

            for (int i = 0; i < decorations.Count; i++)
            {
                decorations[i].Update(elaspedMiliseconds);
            }
            for (int i = 0; i < collidableObjects.Count; i++)
            {
                collidableObjects[i].Update(elaspedMiliseconds);
            }
            for (int i = 0; i < enemies.Count; i++)
            {
                enemies[i].Update(elaspedMiliseconds);
            }
        }


        /// <summary>
        /// Draw
        /// </summary>
        /// <param name="batchy"></param>
        public void Draw(SpriteBatch batchy)
        {
            // draw background
            for (int i = 0; i < tiles.Length; i++)
                batchy.Draw(tileTextures[i%2], tiles[i], Color.White);

            // decoration
            for (int i = 0; i < decorations.Count; i++)
            {
                decorations[i].Draw(batchy);
            }
            for (int i = 0; i < collidableObjects.Count; i++)
            {
                collidableObjects[i].Draw(batchy);
            }

            // draw player
            player1.Draw(batchy);

            // draw enemies
            for (int i = 0; i < enemies.Count; i++)
            {
                enemies[i].Draw(batchy);
            }
        }
    }
}
